package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.DateFormatUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 1、初始化 全程要求完成日期 = 原全程要求完成日期+(解挂的当天日期-挂起日期)
 */
@Service
public class AsideHangCompleteAttrHandler extends AbstractAttrHandler {
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr){
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        String lockOrReleaseOpType = querySceneInstService.getExtParameterValue(sceneInstId, "lockOrReleaseOpType");
        if (KiteStringUtils.equals("RELEASE", lockOrReleaseOpType)) {
            // 挂起时限
            KiteAttr hangDateAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10279");
            if(Objects.nonNull(hangDateAttr)){
                hangDateAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                hangDateAttr.setIsNull(KeyConsts.IFTRUE_T);
                operateSceneCommonService.setCommonAttr(sceneInstId, hangDateAttr);
            }
        } else if (KiteStringUtils.equals("LOCK", lockOrReleaseOpType)) {
            commonAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
            commonAttr.setIsNull(KeyConsts.IFTRUE_T);

        }
        setCommonAttr(commonAttr);
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        if(KiteStringUtils.isEmpty(value)){
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        String lockOrReleaseOpType = querySceneInstService.getExtParameterValue(sceneInstId, "lockOrReleaseOpType");
        if (KiteStringUtils.equals("RELEASE", lockOrReleaseOpType)) {
        // 全程要求完成时间
            // 场景初始化过程中不校验
            String isInitPageRenderFlag = querySceneInstService.getExtParameterValue(sceneInstId, "isInitPageRenderFlag");
            if (null!=isInitPageRenderFlag&&KiteStringUtils.isEqual(isInitPageRenderFlag, LocalKeyConsts.IFTRUE_T)) {
                return super.apply(sceneInstId, fieldName, value, valueDesc);
            }
            KiteAttr allFinishAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10012");
            if (Objects.nonNull(allFinishAttr)) {
                if (DateFormatUtils.compareDate(value, allFinishAttr.getValue(), DateFormatUtils.DATE_FORMAT) > 0) {
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("A、Z端(PE、CE端)要求完成时间不能晚于全程要求完成时间");
                }

                if (DateFormatUtils.compareDate(DateFormatUtils.getFormatedDate(), value, DateFormatUtils.DATE_FORMAT) > 0) {
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("A、Z端(PE、CE端)要求完成时间不能早于当前时间");
                }
            }
            // 挂起时限
            KiteAttr hangDateAttr = querySceneInstService.getCommonAttr(sceneInstId, "ORD10279");
            if(Objects.nonNull(hangDateAttr)){
                hangDateAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                hangDateAttr.setIsNull(KeyConsts.IFTRUE_T);
                operateSceneCommonService.setCommonAttr(sceneInstId, hangDateAttr);
            }
        } else if (KiteStringUtils.equals("LOCK", lockOrReleaseOpType)) {

        }

        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
